package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.service.CommentApi;
import com.tanhua.service.UserInfoApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author げっしょく
 * @date 2021/10/27
 */

@Service
public class CommentsService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private MqMessageService mqMessageService;

    //保存动态的评论
    public void saveComments(String movementId, String comment) {
        Long id = UserHolder.getId();
        Comment comment1 = new Comment();
        comment1.setContent(comment);
        comment1.setUserId(id);
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setCommentType(CommentType.COMMENT.getType());
        comment1.setCreated(System.currentTimeMillis());
        mqMessageService.sendLogService(UserHolder.getId(), "0205", "comment", null);
        commentApi.save(comment1);
    }

    //查询指定动态的评论信息
    public PageResult getComments(Integer page, Integer pagesize, String movementId) {
        //根据动态id去查询评论表
        List<Comment> comments = commentApi.getComments(page, pagesize, new ObjectId(movementId));
        //判断评论表是否为空
        if (comments == null) {
            return new PageResult();
        }
        //不为空拿着评论人的id去查询用户详情表
        List<Long> collect = comments.stream().map(c -> c.getUserId()).collect(Collectors.toList());
        if (CollUtil.isEmpty(collect)) {
            return new PageResult();
        }
        Map map = userInfoApi.getByUserIds(collect, null);
        List<CommentVo> list = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = (UserInfo) map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                //判定用户是否对评论是否进行了点赞
                String key = Constants.COMMENT_INTERACT + comment.getId();
                String hashKey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getId();
                if (redisTemplate.opsForHash().hasKey(key, hashKey)) {
                    vo.setHasLiked(1);
                }
                list.add(vo);
            }
        }
        PageResult pageResult = new PageResult(page, pagesize, 0L, list);
        //合并成一个封装类
        return pageResult;
    }



    //进行对评论的点赞和取消点赞操作
    public Integer commentlike(String id, boolean bool) {
        //根据评论id查找评论
        Boolean b = commentApi.getComment(new ObjectId(id));
        //判断评论是否点赞
        if (bool && b) {
            //如果是进行点赞操作,已点赞就直接响应
            throw new BusinessException(ErrorResult.likeError());
        } else if (!bool && !b) {
            //如果是进行取消点赞操作,未点赞就直接响应
            throw new BusinessException(ErrorResult.disLikeError());
        }
        //进行对应的操作
        Integer integer = null;
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(id));
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setUserId(UserHolder.getId());

        if (bool) {
            //将评论点赞并返回实时数据
            comment.setCreated(System.currentTimeMillis());
            Comment comment1 = commentApi.saveComment(comment);
            integer = comment1.getLikeCount();
            String key = Constants.COMMENT_INTERACT + comment1.getId();
            String hashKey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getId();
            redisTemplate.opsForHash().put(key, hashKey, "1");
            mqMessageService.sendLogService(UserHolder.getId(), "0203", "movement", null);
        } else {
            //将评论取消点赞并返回
            Comment comment1 = commentApi.deleteComment(comment);
            integer = comment1.getLikeCount();
            String key = Constants.COMMENT_INTERACT + comment1.getId();
            String hashKey = Constants.COMMENT_LIKE_HASHKEY + UserHolder.getId();
            redisTemplate.opsForHash().delete(key, hashKey);
            mqMessageService.sendLogService(UserHolder.getId(), "0206", "movement", null);
        }

        return integer;

    }
}
